package com.example.androidshixun;

import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.androidshixun.fragment.FragmentAdapter;
import com.example.androidshixun.fragment.Tab1Fragment;
import com.example.androidshixun.fragment.Tab2Fragment;
import com.example.androidshixun.fragment.Tab3Fragment;
import com.example.androidshixun.fragment.Tab4Fragment;
import com.example.androidshixun.utils.HttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ViewPager viewPager;
    private ImageView img1,img2,img3,img4;
    private TextView tv1,tv2,tv3,tv4;
    public static int requestLogin = 8848;
    private Tab4Fragment tab4Fragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.view_pager);

        List<Fragment> list = new ArrayList<>();
        Tab1Fragment tab1Fragment = new Tab1Fragment();
        list.add(tab1Fragment);

        Tab2Fragment tab2Fragment = new Tab2Fragment();
        list.add(tab2Fragment);

        Tab3Fragment tab3Fragment = new Tab3Fragment();
        list.add(tab3Fragment);

        tab4Fragment = new Tab4Fragment();
        list.add(tab4Fragment);

        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(), list);

        viewPager.setAdapter(adapter);

        LinearLayout tab1 = findViewById(R.id.tab_1);
        tab1.setOnClickListener(this);

        LinearLayout tab2 = findViewById(R.id.tab_2);
        tab2.setOnClickListener(this);

        LinearLayout tab3 = findViewById(R.id.tab_3);
        tab3.setOnClickListener(this);

        LinearLayout tab4 = findViewById(R.id.tab_4);
        tab4.setOnClickListener(this);

        img1 = findViewById(R.id.img_1);
        img2 = findViewById(R.id.img_2);
        img3 = findViewById(R.id.img_3);
        img4 = findViewById(R.id.img_4);

        tv1 = findViewById(R.id.tv_1);
        tv2 = findViewById(R.id.tv_2);
        tv3 = findViewById(R.id.tv_3);
        tv4 = findViewById(R.id.tv_4);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
            @Override
            public void onPageScrollStateChanged(int state) {}

            @Override
            public void onPageSelected(int position) {
                setNormal();
                if(position == 0) {
                    img1.setBackgroundResource(R.drawable.home_page_tab1_selected);
                    tv1.setTextColor(getResources().getColor(R.color.yellow));
                } else if(position == 1) {
                    img2.setBackgroundResource(R.drawable.home_page_tab2_selected);
                    tv2.setTextColor(getResources().getColor(R.color.yellow));
                } else if(position == 2) {
                    img3.setBackgroundResource(R.drawable.home_page_tab3_selected);
                    tv3.setTextColor(getResources().getColor(R.color.yellow));
                } else if(position == 3) {
                    img4.setBackgroundResource(R.drawable.home_page_tab4_selected);
                    tv4.setTextColor(getResources().getColor(R.color.yellow));
                }
            }
        });

        img1.setBackgroundResource(R.drawable.home_page_tab1_selected);
        tv1.setTextColor(getResources().getColor(R.color.yellow));
        String url = "http://121.201.123.243/ggdq/model/getUser?id=0";
        HttpUtils.sendRequest(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                call = call;
                //TODO 处理网络访问失败
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String jsonStr = response.body().string();
                try {
                    JSONObject object = new JSONObject(jsonStr);
                    String name =  object.optString("userName");

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }


    @Override
    public void onClick(View v) {
        //把所有的 img tv 变成灰色
        setNormal();
        switch (v.getId()) {
            case R.id.tab_1:
                viewPager.setCurrentItem(0);
                img1.setBackgroundResource(R.drawable.home_page_tab1_selected);
                tv1.setTextColor(getResources().getColor(R.color.yellow));
                break;
            case R.id.tab_2:
                viewPager.setCurrentItem(1);
                img2.setBackgroundResource(R.drawable.home_page_tab2_selected);
                tv2.setTextColor(getResources().getColor(R.color.yellow));
                break;
            case R.id.tab_3:
                viewPager.setCurrentItem(2);
                img3.setBackgroundResource(R.drawable.home_page_tab3_selected);
                tv3.setTextColor(getResources().getColor(R.color.yellow));
                break;
            case R.id.tab_4:
                viewPager.setCurrentItem(3);
                img4.setBackgroundResource(R.drawable.home_page_tab4_selected);
                tv4.setTextColor(getResources().getColor(R.color.yellow));
                break;
        }
    }

    private void setNormal() {
        img1.setBackgroundResource(R.drawable.home_page_tab1_normal);
        img2.setBackgroundResource(R.drawable.home_page_tab2_normal);
        img3.setBackgroundResource(R.drawable.home_page_tab3_normal);
        img4.setBackgroundResource(R.drawable.home_page_tab4_normal);
        tv1.setTextColor(getResources().getColor(R.color.gary));
        tv2.setTextColor(getResources().getColor(R.color.gary));
        tv3.setTextColor(getResources().getColor(R.color.gary));
        tv4.setTextColor(getResources().getColor(R.color.gary));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == requestLogin) {
            if(data != null) {
                String name = data.getStringExtra(Const.USER_NAME);
                tab4Fragment.setUserNameTv(name);
            }
        }
    }
}
